#include <cstdio>
#include <vector>
#include <cstring>
#define V 20010

using namespace std;

vector<int> vec[ V ];
int id[ V ], pre[ V ], low[ V ], s[ V ], stop, cnt, scnt;
bool flag;

void tarjan( int v, int n )
{
    int t, minc = low[ v ] = pre[ v ] = cnt++;
    vector<int>::iterator pv;
    s[ stop++ ] = v;
    for ( pv = vec[ v ].begin( ); pv != vec[ v ].end( ); pv++ )
    {
        if ( -1 == pre[ *pv ] )
            tarjan( *pv, n );
        else
            if ( pre[ *pv ] != low[ *pv ] )
                flag = 1;
        if ( low[ *pv ] < minc ) minc = low[ *pv ];
    }
    if ( minc < low[ v ] ) { low[ v ] = minc; return ; }
    do {
        id[ t = s[ --stop ] ] = scnt; low[ t ] = n;
    } while ( t != v );
    scnt++;
}

int n;

void init( )
{
    int i, u, v;
    stop = cnt = scnt = 0;
    scanf("%d", &n);
    for ( i = 0; i < n; i++ )
    {
        vec[ i ].clear( );
        pre[ i ] = -1;
    }
    while ( scanf("%d%d", &u, &v) )
    {
        if ( !u && !v ) break;
        vec[ u ].push_back( v );
    }
}

void work( )
{
    int i;
    flag = 0;
    for ( i = 0; i < n; i++ )
        if ( pre[ i ] == -1 )
            tarjan( i, n );
    if ( scnt != 1 || flag )
        puts("NO");
    else
        puts("YES");
}

int main( )
{
    int t;
    scanf("%d", &t);
    while ( t-- )
    {
        init( );
        work( );
    }
    return 0;
}
